package com.tancire.member.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.utils.DateUtils;
import com.tancire.member.domain.Member;
import com.tancire.member.service.IMemberService;
import org.springframework.stereotype.Service;
import com.tancire.member.mapper.MemberTrainAwardMapper;
import com.tancire.member.domain.MemberTrainAward;
import com.tancire.member.service.IMemberTrainAwardService;

/**
 * 会员奖品Service业务层处理
 *
 * @author tancire
 * @date 2024-10-23
 */
@Service
public class MemberTrainAwardServiceImpl implements IMemberTrainAwardService {

    @Resource
    private MemberTrainAwardMapper memberTrainAwardMapper;

    @Resource
    private IMemberService memberService;

    /**
     * 查询会员奖品
     *
     * @param id 会员奖品主键
     * @return 会员奖品
     */
    @Override
    public MemberTrainAward selectMemberTrainAwardById(Long id) {
        return memberTrainAwardMapper.selectMemberTrainAwardById(id);
    }

    /**
     * 查询会员奖品列表
     *
     * @param memberTrainAward 会员奖品
     * @return 会员奖品
     */
    @Override
    public List<MemberTrainAward> selectMemberTrainAwardList(MemberTrainAward memberTrainAward) {
        List<MemberTrainAward> memberTrainAwards = memberTrainAwardMapper.selectMemberTrainAwardList(memberTrainAward);
        // 查询会员信息
        if (ObjectUtil.isNotEmpty(memberTrainAwards)) {
            List<Long> ids = memberTrainAwards.stream().map(MemberTrainAward::getMemberId).collect(Collectors.toList());
            List<Member> memberList = memberService.selectMemberByIds(ids);
            if (ObjectUtil.isNotEmpty(memberList)) {
                for (MemberTrainAward award : memberTrainAwards) {
                    Member member = this.matchMember(memberList, award.getMemberId());
                    if (null != member) {
                        award.setMemberName(member.getNickName());
                    }
                }
            }
        }
        return memberTrainAwards;
    }

    /**
     * 匹配会员信息
     *
     * @param memberList
     * @param memberId
     * @return
     */
    private Member matchMember(List<Member> memberList, Long memberId) {
        if (ObjectUtil.isNotEmpty(memberList)) {
            for (Member member : memberList) {
                if (Objects.equals(member.getMemberId(), memberId)) {
                    return member;
                }
            }
        }
        return null;
    }

    /**
     * 新增会员奖品
     *
     * @param memberTrainAward 会员奖品
     * @return 结果
     */
    @Override
    public int insertMemberTrainAward(MemberTrainAward memberTrainAward) {
        memberTrainAward.setCreateTime(DateUtils.getNowDate());
        return memberTrainAwardMapper.insertMemberTrainAward(memberTrainAward);
    }

    /**
     * 修改会员奖品
     *
     * @param memberTrainAward 会员奖品
     * @return 结果
     */
    @Override
    public int updateMemberTrainAward(MemberTrainAward memberTrainAward) {
        memberTrainAward.setUpdateTime(DateUtils.getNowDate());
        return memberTrainAwardMapper.updateMemberTrainAward(memberTrainAward);
    }

    /**
     * 批量删除会员奖品
     *
     * @param ids 需要删除的会员奖品主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainAwardByIds(Long[] ids) {
        return memberTrainAwardMapper.deleteMemberTrainAwardByIds(ids);
    }

    /**
     * 删除会员奖品信息
     *
     * @param id 会员奖品主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainAwardById(Long id) {
        return memberTrainAwardMapper.deleteMemberTrainAwardById(id);
    }
}
